package com.rt.web.controller.system;

import java.util.List;

import com.rt.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rt.common.annotation.Log;
import com.rt.common.core.controller.BaseController;
import com.rt.common.core.domain.AjaxResult;
import com.rt.common.enums.BusinessType;
import com.rt.system.domain.RtSubject;
import com.rt.system.service.IRtSubjectService;
import com.rt.common.utils.poi.ExcelUtil;
import com.rt.common.core.page.TableDataInfo;

/**
 * 科目Controller
 * 
 * @author Yg
 * @date 2022-01-05
 */
@RestController
    @RequestMapping("/system/subject")
public class RtSubjectController extends BaseController
{
    @Autowired
    private IRtSubjectService rtSubjectService;

    /**
     * 查询科目列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(RtSubject rtSubject)
    {
        startPage();
        List<RtSubject> list = rtSubjectService.selectRtSubjectList(rtSubject);
        return getDataTable(list);
    }

    /**
     * 查询可用科目列表
     */
    @GetMapping("/availableList")
    public TableDataInfo availableList(RtSubject rtSubject)
    {
        rtSubject.setStatus(1);
        List<RtSubject> list = rtSubjectService.selectRtSubjectList(rtSubject);
        return getDataTable(list);
    }

    /**
     * 导出科目列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:export')")
    @Log(title = "科目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RtSubject rtSubject)
    {
        List<RtSubject> list = rtSubjectService.selectRtSubjectList(rtSubject);
        ExcelUtil<RtSubject> util = new ExcelUtil<RtSubject>(RtSubject.class);
        return util.exportExcel(list, "科目数据");
    }

    /**
     * 获取科目详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:subject:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rtSubjectService.selectRtSubjectById(id));
    }

    /**
     * 新增科目
     */
    @PreAuthorize("@ss.hasPermi('system:subject:add')")
    @Log(title = "科目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RtSubject rtSubject)
    {
        if (StringUtils.isNotNull(rtSubjectService.selectRtSubjectByName(rtSubject))){
            return AjaxResult.error("科目或编码已存在");
        }
        rtSubject.setCreateOperator(getUsername());
        return toAjax(rtSubjectService.insertRtSubject(rtSubject));
    }

    /**
     * 修改科目
     */
    @PreAuthorize("@ss.hasPermi('system:subject:edit')")
    @Log(title = "科目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RtSubject rtSubject)
    {
        if (StringUtils.isNotNull(rtSubjectService.selectRtSubjectByName(rtSubject))){
            return AjaxResult.error("科目或编码已存在");
        }
        rtSubject.setUpdateOperator(getUsername());
        return toAjax(rtSubjectService.updateRtSubject(rtSubject));
    }

    /**
     * 删除科目
     */
    @PreAuthorize("@ss.hasPermi('system:subject:remove')")
    @Log(title = "科目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rtSubjectService.deleteRtSubjectByIds(ids));
    }
}
